﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp2
{
    public class PagedResultViewModel:ViewModelBase
    {
        #region 无参构造函数
        public PagedResultViewModel()
        {
            FirstPageCommand = new RelayCommand(FirstPage);
            LastPageCommand = new RelayCommand(LastPage);
            PreviousPageCommand = new RelayCommand(PreviousPage);
            NextPageCommand = new RelayCommand(NextPage);
        }
        #endregion

        #region 两个Action委托
        public Action<bool> Alert { set; get; }
        public Action<int> AlertTotalCount { set; get; }
        #endregion

        #region 学生类集合
        public List<Students> List { get; set; }

        #endregion

        #region 普通属性
        public string Table { set; get; }
        public bool CanGoToFirstPage => CurrentPage > 1;
        public bool CanGoToPreviousPage => CurrentPage > 1;
        public bool CanGoToNextPage => CurrentPage < TotalPages;
        public bool CanGoToLastPage => CurrentPage < TotalPages;

        //页面大小选项
        public List<int> PageSizeOptions { get; set; } = new() { 100, 150, 200, 250, 300 };
        //总页数
        public int TotalPages => (int)Math.Ceiling((double)TotalCount / PageSize);
        //页面显示
        public string PageDisplay => $"第 {CurrentPage} 页 / 共 {TotalPages} 页 (共 {TotalCount} 条)";
        //sql过滤条件
        public string Filter { get; set; } = "1=1";
        #endregion

        #region 动态属性
        //总计数量
        private int _totalCount;
        public int TotalCount
        {
            get => _totalCount;
            set => SetProperty(ref _totalCount, value);
        }
        //当前页数
        private int _currentPage = 1;
        public int CurrentPage
        {
            get => _currentPage;
            set => SetProperty(ref _currentPage, value);
        }
        //每页记录条数
        private int _pageSize = 100;
        public int PageSize
        {
            get => _pageSize;
            set => SetProperty(ref _pageSize, value);
        }

        private ObservableCollection<Students> _item;
        public ObservableCollection<Students> Items
        {
            get => _item;
            set => SetProperty(ref _item, value);
        }
        #endregion

        #region 命令
        public RelayCommand FirstPageCommand { get; set; }

        public RelayCommand LastPageCommand { get; set; }

        public RelayCommand PreviousPageCommand { get; set; }

        public RelayCommand NextPageCommand { get; set; }

        #endregion

        #region 命令实现方法
        public void FirstPage()
        {
            CurrentPage = 1;
            Task.Run(async () => { await RefreshPage(); });
        }

        public void LastPage() 
        {
            //当前页数等于总页数
            CurrentPage = TotalPages;
            Task.Run(async () => { await RefreshPage(); });
        }

        private void PreviousPage()
        {
            CurrentPage--;
            Task.Run(async () => { await RefreshPage(); });
        }

        private void NextPage()
        {
            if (CurrentPage < TotalPages)
            {
                CurrentPage++;   
            }
            Task.Run(async () => { await RefreshPage(); });
        }

        #endregion




        public async Task RefreshPage()
        {

            if (List == null)
            {
                //输入当前页数，每页记录数，过滤条件，从数据库中获取数据
                var item = await SqlHelper.GetDataWithCountAsync(CurrentPage, PageSize, Filter);
                //动态属性Students类集合
                Items = new ObservableCollection<Students>(item.Item3);
                App.Current.Dispatcher.Invoke(() =>
                {
                    CurrentPage = item.Item1;
                    TotalCount = item.Item2;
                });
            }
            else
            {
                var list = List.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList();
                Items = new ObservableCollection<Students>(list);
                App.Current.Dispatcher.Invoke(() =>
                {
                    TotalCount = List.Count;
                });
            }
            if (Items.Count == 0)
                Alert?.Invoke(false);
            else
                Alert?.Invoke(true);

            App.Current.Dispatcher.Invoke(() =>
            {
                OnPropertyChanged(nameof(PageDisplay));
                OnPropertyChanged(nameof(CanGoToFirstPage));
                OnPropertyChanged(nameof(CanGoToPreviousPage));
                OnPropertyChanged(nameof(CanGoToNextPage));
                OnPropertyChanged(nameof(CanGoToLastPage));
                FirstPageCommand.NotifyCanExecuteChanged();
                PreviousPageCommand.NotifyCanExecuteChanged();
                NextPageCommand.NotifyCanExecuteChanged();
                LastPageCommand.NotifyCanExecuteChanged();
            });
            AlertTotalCount?.Invoke(TotalCount);
        }
    }
}
